package com.yx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yx.dao.ComplaintMapper;
import com.yx.dao.ComplaintTypeMapper;
import com.yx.dao.OwnerMapper;
import com.yx.model.Complaint;
import com.yx.model.ComplaintType;
import com.yx.model.Owner;
import com.yx.service.IComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class ComplaintServiceImpl extends ServiceImpl<ComplaintMapper, Complaint> implements IComplaintService {

    @Autowired
    private ComplaintMapper complaintDao;

    @Autowired
    private OwnerMapper ownerMapper;

    @Autowired
    private ComplaintTypeMapper complaintTypeMapper;

    @Override
    public IPage<Complaint> findComplaintAll(IPage<Complaint> iPage, Complaint complaint) {
        // 获取封装条件
        String comId = complaint.getComId();
        Integer status = complaint.getStatus();
        QueryWrapper<Complaint> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(comId)) {
            wrapper.eq("com_id", comId);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        // 然后进行查询数据
        IPage<Complaint> complaintIPage = complaintDao.selectPage(iPage, wrapper);

        // 进行数据封装
        List<Complaint> records = complaintIPage.getRecords();

        records = records.stream().map(complaint1 -> {
            Integer ownerId = complaint1.getOwnerId();
            Owner owner = ownerMapper.selectById(ownerId);
            complaint1.setOwner(owner);

            // 投诉类型封装
            String comId1 = complaint1.getComId();
            ComplaintType complaintType = complaintTypeMapper.selectById(comId1);
            complaint1.setType(complaintType);
            return complaint1;
        }).collect(Collectors.toList());

        // 封装回数据
        complaintIPage.setRecords(records);
        // 返回数据
        return complaintIPage;
    }

    @Override
    public PageInfo<Complaint> findComplaintAll(int page, int pagesize, Complaint complaint) {

        // 封装数据
        Page<Complaint> complaintPage = new Page<>(page, pagesize);
        String comId = complaint.getComId();
        Integer status = complaint.getStatus();
        Integer ownerId = complaint.getOwnerId();
        QueryWrapper<Complaint> wrapper = new QueryWrapper<>();
        wrapper.eq("owner_id", ownerId);

        if (!StringUtils.isEmpty(comId)) {
            wrapper.eq("com_id", comId);
        }

        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }

        // 查询出来数据
        Page<Complaint> selectPage = complaintDao.selectPage(complaintPage, wrapper);

        // 进行数据封装
        PageInfo<Complaint> pageInfo = new PageInfo<>();

        // 细节处理数据
        List<Complaint> records = selectPage.getRecords();
        records = records.stream().map(complaint1 -> {
            Integer ownerId1 = complaint1.getOwnerId();
            Owner owner = ownerMapper.selectById(ownerId1);
            complaint1.setOwner(owner);

            // 投诉类型封装
            String comId1 = complaint1.getComId();
            ComplaintType complaintType = complaintTypeMapper.selectById(comId1);
            complaint1.setType(complaintType);

            return complaint1;
        }).collect(Collectors.toList());

        pageInfo.setList(records);

        pageInfo.setTotal(selectPage.getTotal());

        // 返回数据
        return pageInfo;
    }

    @Override
    public IPage<Complaint> findListByPage(Integer page, Integer pageCount) {
        IPage<Complaint> wherePage = new Page<>(page, pageCount);
        Complaint where = new Complaint();

        return baseMapper.selectPage(wherePage, Wrappers.query(where));
    }


    @Override
    public int add(Complaint complaint) {
        return baseMapper.insert(complaint);
    }

    @Override
    public int delete(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(Complaint complaint) {
        return baseMapper.updateById(complaint);
    }

    @Override
    public Complaint findById(Long id) {
        return baseMapper.selectById(id);
    }
}
